package com.zzt.questionbankquestion.controller;

import com.zzt.questionbankquestion.bean.dto.*;
import com.zzt.questionbankquestion.bean.dto.query.QuestionTypeQueryDTO;
import com.zzt.questionbankquestion.service.QuestionTypeModeService;
import com.zzt.questionbankquestion.service.QuestionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/questionType")
public class QuestionTypeController {

    @Autowired
    private QuestionTypeService questionTypeService;

    @Autowired
    private QuestionTypeModeService questionTypeModeService;

    @RequestMapping("/addQuestionType")
    public Result addQuestionType(@RequestBody QuestionTypeModeListDTO questionTypeModeListDTO){
        return questionTypeModeService.addQuestionTypeMode(questionTypeModeListDTO);
    }


    @RequestMapping("/deleteQuestionType")
    public Result deleteQuestion(@RequestBody long questionTypeId){
        return questionTypeModeService.deleteByType(questionTypeId);
    }

    @RequestMapping("/getQuestionType")
    public Result<QuestionTypeModeListDTO> getQuestionType(@RequestBody long questionTypeId){
        return questionTypeModeService.getByTypeId(questionTypeId);
    }

    @RequestMapping("/getAPage")
    public PageResult<QuestionTypeDTO> getAPage(@RequestBody PageRequestMessage pageRequestMessage){
        if(pageRequestMessage.getPage()==null||pageRequestMessage.getPage()<2){
            pageRequestMessage.setPage(0);
        }
        else {
            pageRequestMessage.setPage(pageRequestMessage.getPage()-1);
        }
        if(pageRequestMessage.getSize()==null||pageRequestMessage.getSize()<0){
            pageRequestMessage.setSize(8);
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest pageRequest = PageRequest.of(pageRequestMessage.getPage(), pageRequestMessage.getSize(), sort);
        return questionTypeService.getAPage(pageRequest);
    }

    @RequestMapping("/getASpecialPage")
    public PageResult<QuestionTypeDTO> getASpecialPage(@RequestBody QuestionTypeQueryDTO dto){
        System.out.println(dto);
        if(dto.getPage()==null||dto.getPage()<2){
            dto.setPage(0);
        }
        else {
            dto.setPage(dto.getPage()-1);
        }
        if(dto.getSize()==null||dto.getSize()<0){
            dto.setSize(8);
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest pageRequest = PageRequest.of(dto.getPage(), dto.getSize(), sort);
        return questionTypeService.getASpecialPage(dto.getWhereClause(),pageRequest);
    }

}
